﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using XYTXL.AppCode;
using XYTXL.Data;
using System.Text.RegularExpressions;
using XYTXL.API;

namespace XYTXL.View
{
    public partial class MangerUser : BasePage
    {
        public string strAction
        {
            get
            {
                return Request.QueryString["Action"].ToString();
            }
        }

        public string strBanchCode
        {
            get
            {
                return Request.QueryString["PID"] ?? "-1";
            }
        }

        public string strUserName
        {
            get
            {
                return Request.QueryString["UserName"] ?? "-1";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialData();
                //btAddUser.Attributes.Add("onclick", "this.value='正在提交中,請稍等……';this.disabled=true;" + this.GetPostBackEventReference(btAddUser) + ";");

            }
        }

        protected void btAddUser_Click(object sender, EventArgs e)
        {
            if (ChenckData())
            {
                if (strAction == "add")
                {
                    Regex regex = new Regex(@"");

                    //if (!Regex.IsMatch(tbLogonName.Text, "^0?1[3|4|7|5|8][0-9]\\d{8}$"))
                    //{
                    //    AlertMSG("4", "请输入正确的用户名手机号！");
                    //    return;
                    //}
                    //if (!Regex.IsMatch(conPhone.Text.Trim(), "^0?1[3|4|7|5|8][0-9]\\d{8}$"))
                    //{
                    //    AlertMSG("4", "请输入正确的手机号！");
                    //    return;
                    //}
                    //tbLogonName.Text = conPhone.Text;
                    string tbName=tbLogonName.Text.ToString().Trim();
                    List<JH_Auth_User> User = new JH_Auth_UserB().GetEntities(d => d.UserName == tbName).ToList();
                    if (User.Count() > 0)
                    {
                        AlertMSG("4", "已经存在相同用户！");

                    }
                    else
                    {
                        JH_Auth_User NewUser = new JH_Auth_User();
                        NewUser.UserName = tbLogonName.Text.ToString().Trim();
                        NewUser.UserRealName = tbRealName.Text.ToString().Trim();
                        NewUser.zhiwu = tbZhiWu.Text.ToString();
                        NewUser.UserPass = "123";
                        NewUser.IsUse = ckIsUser.Checked ? "Y" : "N";
                        NewUser.tx = "TX10";
                        NewUser.BranchCode = int.Parse(ddlBranch.SelectedValue);
                        NewUser.STATUS = "";
                        NewUser.Usersign = conGWSM.Text;
                        NewUser.remark = new JH_Auth_BranchB().GetEntity(d => d.DeptCode == NewUser.BranchCode).Remark1;
                        //NewUser.weixinnum = conWXNum.Text;
                        NewUser.mailbox = conYX.Text;
                        NewUser.mobphone = conPhone.Text.Trim();
                        new JH_Auth_UserB().Insert(NewUser);
                        //新增人员默认为一般角色
                        new JH_Auth_UserRoleB().Insert(new JH_Auth_UserRole() { RoleCode = 3, UserName = NewUser.UserName });

                        try
                        {
                            AlertMSG("3", "添加用户成功！");

                        }
                        catch (Exception)
                        {
                            AlertMSG("4", "添加用户失败！");
                            new JH_Auth_UserB().Delete(NewUser);
                        }
                    }

                }
                else
                {
                    JH_Auth_User UserUP = new JH_Auth_UserB().GetEntities(d => d.UserName == strUserName).SingleOrDefault();
                    UserUP.UserRealName = tbRealName.Text.ToString().Trim();
                    UserUP.BranchCode = int.Parse(ddlBranch.SelectedValue);
                    UserUP.zhiwu = tbZhiWu.Text.ToString();
                    UserUP.IsUse = ckIsUser.Checked ? "Y" : "N";
                    //conWXNum.Text = UserUP.weixinnum;
                    //conYX.Text=UserUP.mailbox;
                    UserUP.Usersign = conGWSM.Text;
                    UserUP.mobphone = conPhone.Text;

                    try
                    {
                        new JH_Auth_UserB().Update(UserUP);
                        AlertMSG("3", "修改用户成功！");
                    }
                    catch (Exception)
                    {
                        AlertMSG("4", "修改用户失败！");
                    }
                }
            }
            else
            {
                return;
            }
        }

        /// <summary>
        /// 服务器端验证
        /// </summary>
        /// <returns></returns>
        public bool ChenckData()
        {
            bool boolcheck = true;

            if (new JH_Auth_UserB().GetEntities(d => d.UserName == strUserName).Count() >= 1 && strAction == "add")
            {
                AlertMSG("4", "已存在相同用户名！");

                boolcheck = false;
            }
            if (conPhone.Text == "")
            {
                AlertMSG("4", "手机号不能为空！");
                boolcheck = false;
            }
            return boolcheck;
        }

        public void InitialData()
        {
            new CommonHelp.BindData().BindBranch(ddlBranch, "Y");
            if (strAction == "add")
            {
                ddlBranch.SelectedValue = strBanchCode;
            }
            else
            {

                JH_Auth_User User = new JH_Auth_UserB().GetEntities(d => d.UserName == strUserName).SingleOrDefault();
                tbLogonName.Enabled = false;
                tbLogonName.Text = User.UserName;
                tbRealName.Text = User.UserRealName;

                ckIsUser.Checked = User.IsUse == "Y" ? true : false;
                ddlBranch.SelectedValue = User.BranchCode.ToString();
                tbZhiWu.Text = User.zhiwu;

                //  conWXNum.Text = User.weixinnum;
                conYX.Text = User.mailbox;
                conPhone.Text = User.mobphone;
                //conPhone.Enabled = false;
                conGWSM.Text = User.Usersign;

            }
        }
    }
}